<?php

namespace App\Hospital\Controllers;

use App\Hospital\Repositories\InterviewRemind;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

class InterviewRemindController extends AdminController
{
    protected function description()
    {
        $this->description['index'] = '系统根据签约孕产妇末次月经推送追访填写提醒，用户未填写则会在此表中显示';


        return $this->description;
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        return Grid::make(new InterviewRemind(['user','family','familyPreg']), function (Grid $grid) {
            $grid->model()
                ->where('hospital_id',Admin::user()->hospital_id)
                ->whereDoesntHave('interview',function (Builder $query){
                    $query->where('eb_interview.type', DB::raw("eb_interview_remind.type"));
                })->orderBy('created_at', 'desc');
            $grid->column('family.name','孕产妇姓名');
            $grid->column('user.phone','联系电话');
            $grid->column('familyPreg.last_menses','末次月经时间');
            $grid->column('type')->using(\App\Models\Interview::$typeTxt);
            $grid->column('created_at','提醒时间');

            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableQuickEditButton();
            $grid->disableRowSelector();
            $grid->disableActions();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand(false);
                $filter->equal('family.name','孕产妇姓名');
                $filter->equal('user.phone','联系电话');
                $filter->equal('type')->select(\App\Models\Interview::$typeTxt);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new InterviewRemind(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('family_id');
            $show->field('family_preg_id');
            $show->field('type');
            $show->field('status');
            $show->field('hospital_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new InterviewRemind(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('family_id');
            $form->text('family_preg_id');
            $form->text('type');
            $form->text('status');
            $form->text('hospital_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
